<?php

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    class HT_Model_administrator_models_contest extends Zend_Db_Table {//ten class fai viet hoa

        protected $_db;

        public function __construct() {
            $this->_name = "contest";
            $this->_db = Zend_Registry::get('dbMain');
            parent::init();
        }
        public function getAll($where){
            $sql = "select * from contest where ".$where;                        
            $retval = $this->_db->fetchAll($sql);        
            return $retval;
        }
        public function getAllExtend($where, $user_id){
            $sql = "select Co.*,
            (select count(Vo.id) from vouchers Vo where Vo.coupon_id = Co.id and Vo.voucher_status = 1) as total_used,
            (select Pa.location_id from partners Pa where Co.partner_id = Pa.id) as location_id,
            (SELECT COUNT(*) FROM coupon_like Cl WHERE Cl.coupon_id = Co.id AND Cl.status = 1 AND Cl.user_id = ".$user_id.") as liked
            from coupons Co where ".$where;                        
            $retval = $this->_db->fetchAll($sql);        
            return $retval;
        }
        public function findById($id){
            $sql = "select * from contest where id = $id";
            $retval = $this->_db->fetchRow($sql);
            return $retval;
        }
        public function getcouponitem($coupon_id,$user_id) {
            $sql = "SELECT *, (SELECT COUNT(*) FROM coupon_like WHERE coupon_id = coupons.id AND `status` = 1 AND coupon_like.user_id = ".$user_id.") AS liked FROM coupons WHERE id = ".$coupon_id;            
            $retval = $this->_db->fetchAll($sql, array($coupon_id));
            return $retval[0];
        }
		
		public function delInCoupon_serphase($coupon_id) {
			return $this->_db->delete('coupon_serphase',"coupon_id = $coupon_id");
		}
		
		public function insertInCoupon_serphase($data) {
			return $this->_db->insert('coupon_serphase', $data);
		}
		
		public function getCoupon_serphase($where="1=1"){
			$select = $this->_db->select()
								->from('coupon_serphase')
								->where($where);
			$result = $this->_db->fetchAll($select);
			return $result;									
		}				        

    }

?>
